<?php

namespace Apps\Common\Libs;

use Apps\Common\Models\SysSmsCode;
use Phalcon\Db\RawValue;
use PhalconPlus\Common\Util;

/**
 * 短信处理
 * @package Apps\Common\Libs
 */
class MobileSMS
{
    /**
     * 检验验证码
     * @param $type
     * @param $code
     * @param $mobile
     * @param $accessToken
     * @return bool
     */
    public static function Check($type, $code, $mobile, $accessToken)
    {
        //return $code == '1111';

        return SysSmsCode::count([
            'conditions' => 'code_type = :type: AND access_token_id = :atid: AND mobile_num = :mobile: AND code_state = 1 AND code_info = :code:',
            'bind' => [
                'type' => $type,
                'atid' => $accessToken,
                'mobile' => $mobile,
                'code' => $code,
            ]
        ]) > 0 ? true : false;
    }

    /**
     * 发送验证码
     * @param $type
     * @param $mobile
     * @param $accessToken
     * @return bool
     */
    public static function Send($type, $mobile, $accessToken)
    {
        // 查询之前是否有未失效的验证码
        $exist = SysSmsCode::findFirst([
            'conditions' => 'code_type = :type: AND access_token_id = :atid: AND mobile_num = :mobile: AND code_state = 1',
            'bind' => [
                'type' => $type,
                'atid' => $accessToken,
                'mobile' => $mobile
            ]
        ]);
        if($exist) {
            return self::SendSMS($exist->code_id);
        }
        // 添加新的
        $code = rand(1000, 9999);
        $sms = new SysSmsCode();
        $sms->access_token_id = $accessToken;
        $sms->mobile_num = $mobile;
        $sms->code_info = $code;
        $sms->code_type = $type;
        if(!$sms->save()) {
            return false;
        }
        return self::SendSMS($sms->code_id);
    }

    /**
     * 发送短信
     * @param $id
     * @return bool|int
     */
    private static function SendSMS($id)
    {
        $codeInfo = SysSmsCode::findFirst("code_id = " . $id);
        if (!$codeInfo) {
            return false;
        }
        $mobileNum = $codeInfo->mobile_num;
        $code = $codeInfo->code_info;
        $company = '【美粒儿】';
        if ($codeInfo->code_type == 'register') {
            $msg = $company . '注册验证码：' . $code . '，您正在使用美粒儿平台进行注册验证，请勿向任何人提供您收到的短信验证码';
        } elseif ($codeInfo->code_type == 'update_password') {
            $msg = $company . '修改密码验证码：' . $code . '，您正在使用美粒儿平台进行修改密码验证，请勿向任何人提供您收到的短信验证码';
        } elseif ($codeInfo->code_type == 'update_mobile') {
            $msg = $company . '绑定手机验证码：' . $code . '，您正在使用美粒儿平台进行绑定手机验证，请勿向任何人提供您收到的短信验证码';
        } else {
            $msg = $company . "您的验证码是$code";
        }
        // 执行发送
        $response = self::SendSMSYunPian($mobileNum, $msg);
        // 保存发送信息
        $codeInfo->send_response = $response;
        $codeInfo->send_time = new RawValue('NOW()');
        $codeInfo->save();
        return $id;
    }

    private static function SendSMSYunPian($mobile, $msg)
    {
        $url = 'http://yunpian.com/v1/sms/send.json';
        $params = [
            'apikey' => 'd532d8295db6b866e6eb33be32c19047',
            'mobile' => $mobile,
            'text' => $msg,
        ];
        return Util::HttpPost($url, $params);
    }
}